﻿using Company.Application.Share.Entities;
using Company.Application.Share.Interfaces;
using Company.Core;
using Company.Core.Models;
using Company.Hardware.Base.Extensions;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Company.Application.Station.ViewModels
{
    internal class DeviceViewModel : BindableBase, INavigationAware
    {
        public HashSet<Type>? Types { get; set; }
        public List<ProtocolType> ProtocolTypes { get; } = Enum.GetValues(typeof(ProtocolType)).Cast<ProtocolType>().ToList();
        private DeviceEntity? device;
        public DeviceEntity? Device
        {
            get => device;
            set => SetProperty(ref device, value);
        }
        public ICommand UpdateDeviceCommand { get; }
        public IStationManager StationManager { get; }
        public IModuleManager ModuleManager { get; }
        private IWebManager WebManager { get; }
        public DeviceViewModel(
            IWebManager webManager, 
            IStationManager stationManager, 
            IModuleManager moduleManager)
        {
            WebManager = webManager;
            ModuleManager = moduleManager;
            StationManager = stationManager;
            UpdateDeviceCommand = new DelegateCommand(OnUpdateDeviceCommand);
            Types = ModuleManager.Get();
        }

        //更新设备信息
        private async void OnUpdateDeviceCommand()
        {
            var result = await WebManager.UpdateAsync<Result<DeviceEntity>>(Device);
            if (result != null && result.Success)
            {
                MessageBox.Show("更新成功");
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        //离开的时候
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        //进来的时候
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var station = navigationContext.Parameters["StationEntity"] as StationEntity; //获取上一个页面转进来的参数
            //StationManager.Station = station;
        }
    }
}
